<?php
namespace App\Service\VendorClient\CommonDataService;
use Service\AppService\IdService;
use App\Service\AppService\SystemCacheConfigService;
use App\Service\BaseService;
use App\Service\Code\Normal;
use App\Service\TaskServer\TaskWorker;

/**
 * 获取公共数据接口配置
 * @author Administrator
 *
 */
class CommonDataApiClient extends BaseService
{
    /**
     * 配置
     * @var 
     */
    public $config;

    /**
     * 默认配置
     * @var  
     */
    private $default_config = 
    [
        'base_url'  =>  'http://servicesharecenter.mboomtech.com'
    ];
    
    /**
     * routersMap
     * @var array 
     */
    private $routers=[
        'queryRegionList'         => '/RegionService/QueryRegionList',
        'queryRegionByAddress'    => '/RegionService/QueryRegionHasParsedByGaoDeAddress',
        'queryServiceConfig'      => '/ServerConfig/queryServiceConfig',
        'queryThirdPartyPlatform' => '/ThirdPartyplatform/QueryThirdPartyplatformList'
    ];
    
    /**
     * 初始参数
     */
    public function __construct()
    {
        $this->config = SystemCacheConfigService::instance()->getCacheInfoBySystemCode('common_data_server');
        if( empty($this->config) ) {
            $this->config = $this->default_config;
        }
    }    

    /**
     * 查询下单接口省市区详情地址
     * @param array $param
     */
    public function queryRegionByAddress($param)
    {
        $request_param=[
            'data'          => $param,
            'method'        => 'queryRegionByAddress',
            'requestMethod' => 'POST'
        ];
        return $this->execute($request_param);
    }

    /**
     * 查询下单接口省市区详情地址
     * @param array $param
     */
    public function queryThirdPartyPlatform($param)
    {
        $request_param=[
            'data'          => $param,
            'method'        => 'queryThirdPartyPlatform',
            'requestMethod' => 'POST'
        ];
        return $this->execute($request_param);
    }
    
    /**
     * 获取第三方业务对接配置信息
     * @param string $service_code
     * @param int $service_type
     * @param string $service_platform_value
     * @param boolean $is_single_field
     * @return mixed
     */
    public function queryServiceConfig( string $service_code , int $service_type ,string $service_platform_value, bool $is_single_field = true  )
    {
        $param = [
            'service_code'              => $service_code,
            'service_type'              => $service_type,
            'service_platform_value'    => $service_platform_value,
            'is_single_field'           => $is_single_field
        ];
        $request_param = [
            'data'          => $param,
            'method'        => 'queryServiceConfig',
            'requestMethod' => 'POST'
        ];
        return $this->execute($request_param);
    }
    
    /**
     * CURL请求
     * @param $params
     * @return mixed
     */
    public function execute( $param = [] )
    {
        if( empty($this->config['base_url']) ) {
            return Normal::returnCode(Normal::FAIL,"当前仓库参数baseurl未配置");
        }

        if( empty($this->routers[$param['method']]) ) {
            return Normal::returnCode(Normal::FAIL,"当前仓库参数【{$param['method']}】未配置");
        }
        $url = $this->config['base_url'].$this->routers[$param['method']];
        $parameter = [
            [
                'tag'    => 'common_data_server_' . $param['method'],
                'uid'    => IdService::instance()->getOtherID(),
                'url'    => $url,
                'data'   => $param['data'],
                'method' => $param['requestMethod'],
                'format' => 'json',
                'header' => [],
            ],
        ];
        $resTask    = TaskWorker::instance()->addTask($parameter);
        $resTask    = current($resTask);
        $httpCode   = $resTask['response']['httpCode'];
        $result     = @json_decode($resTask['response']['result'], true);
        $errorMsg   = $result['msg']?? '请求失败';
        if( isset($result['code']) ) {
            return $result;
        }
        
        return Normal::returnCode(Normal::FAIL,"服务中心接口请求失败，请稍后再试！！！");
    }
}